#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/2 13:14
# @Author  : Yunhao.Cao
# @File    : main.py
from __future__ import absolute_import, unicode_literals
from time import sleep
import re
import urllib
import requests
from bs4 import BeautifulSoup

__author__ = 'Yunhao.Cao'

__ALL__ = []


def main():
    """
    解析页面：https://creditcard.bankcomm.com/content/pccc/discount/activitylist/more.html
    提取标题，标题链接，活动日期起始时间、结束时间

    * BeautifulSoup初始化
    * find方法和find_all方法
    * 图片保存
    :return:
    """
    # 列表页爬虫
    root_url = "https://creditcard.bankcomm.com"
    list_url = root_url + "/content/pccc/discount/activitylist/more.html"

    # 获取列表页soup
    response = requests.get(list_url)
    soup = BeautifulSoup(response.text, "html.parser")

    # 获取列表
    act_list = soup.find("div", class_="actlist")
    li_list = act_list.find_all("li", class_=re.compile("clearfix"))

    detail_url_list = []

    # 解析列表里的url
    for li in li_list:
        a = li.find("a")
        detail_url = a.get("href")
        detail_url = root_url + detail_url

        print detail_url
        detail_url_list.append(detail_url)

    # 图片顺序，从0开始
    i = 0
    # 详情页爬虫
    for detail_url in detail_url_list:
        print 'Sleep 5 seconds...'
        sleep(5)

        print "detail_url = {}".format(detail_url)

        # 获取详情页soup
        response = requests.get(detail_url)
        detail_soup = BeautifulSoup(response.text, "html.parser")

        # 打印title
        print "title = {}".format(detail_soup.title.text)

        # 获取图片url
        img = detail_soup.find("img", alt="file")
        img_src = img.get("src")
        img_src = root_url + img_src
        print "img_src = {}".format(img_src)

        # 获取指定文本
        text_element = detail_soup.find("div", class_="thsh_txt")
        text_content = text_element.text
        print "text = {}".format(text_content)

        # 保存图片
        urllib.urlretrieve(img_src, 'images/{}.jpg'.format(i))
        print "图片保存成功。[{}]".format(i)
        i += 1


if __name__ == '__main__':
    main()
